package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.dto.EventPlan;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventQd;
import com.ruoyi.event.domain.bo.EventQdBo;
import com.ruoyi.event.domain.vo.EventQdVo;
import com.ruoyi.event.mapper.EventQdMapper;
import com.ruoyi.event.service.IEventQdService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 签到Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventQdServiceImpl implements IEventQdService {

    private final EventQdMapper baseMapper;

    /**
     * 查询签到
     */
    @Override
    public EventQdVo queryById(Long qdId) {
        return baseMapper.selectVoById(qdId);
    }

    /**
     * 查询签到列表
     */
    @Override
    public TableDataInfo<EventQdVo> queryPageList(EventQdBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventQd> lqw = buildQueryWrapper(bo);
        Page<EventQdVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询签到列表
     */
    @Override
    public List<EventQdVo> queryList(EventQdBo bo) {
        LambdaQueryWrapper<EventQd> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventQd> buildQueryWrapper(EventQdBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventQd> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventQd::getUserId, bo.getUserId());
        lqw.eq(bo.getMemberId() != null, EventQd::getMemberId, bo.getMemberId());
        lqw.eq(StringUtils.isNotBlank(bo.getQddz()), EventQd::getQddz, bo.getQddz());
        lqw.eq(StringUtils.isNotBlank(bo.getImg()), EventQd::getImg, bo.getImg());
        lqw.eq(StringUtils.isNotBlank(bo.getSp()), EventQd::getSp, bo.getSp());
        lqw.eq(bo.getLng() != null, EventQd::getLng, bo.getLng());
        lqw.eq(bo.getLat() != null, EventQd::getLat, bo.getLat());
        lqw.ge(bo.getStart() != null, EventQd::getCreateTime, bo.getStart());
        lqw.le(bo.getEnd() != null, EventQd::getCreateTime, bo.getEnd());
        return lqw;
    }

    /**
     * 新增签到
     */
    @Override
    public Boolean insertByBo(EventQdBo bo) {
        EventQd add = BeanUtil.toBean(bo, EventQd.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setQdId(add.getQdId());
        }
        return flag;
    }

    /**
     * 修改签到
     */
    @Override
    public Boolean updateByBo(EventQdBo bo) {
        EventQd update = BeanUtil.toBean(bo, EventQd.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventQd entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除签到
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void renderEventInfo(Long userId, List<EventPlan> eventPlanList) {
        LambdaQueryWrapper<EventQd> lqw = Wrappers.lambdaQuery();
        lqw.eq(EventQd::getUserId, userId);
        lqw.orderByAsc(EventQd::getCreateTime);
        List<EventQd> eventJfList = baseMapper.selectList(lqw);
        for (EventPlan eventPlan : eventPlanList) {
            for (EventQd eventQd : eventJfList) {
                if (eventPlan.isOccurInThisPlanCycle(eventQd.getCreateTime())) {
                    eventPlan.setEventId(eventQd.getQdId());
                    eventPlan.setEventDate(eventQd.getCreateTime());
                }
            }
        }

    }
}
